package manong.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import manong.beans.*;
import manong.constant.ServerResponse;
import manong.mapper.CourseMapper;
import manong.mapper.ScoreMapper;
import manong.mapper.StuMapper;
import manong.service.IScoreService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Service
public class ScoreServiceImpl implements IScoreService {
	@Autowired
	private ScoreMapper scoreMapper;
	@Autowired
	private StuMapper stuMapper;
	@Autowired
	private CourseMapper courseMapper;

	@Override
	public ServerResponse<PageInfo<Score>> findAll(Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Score> list = scoreMapper.findAll();
		PageInfo<Score> pageInfo = new PageInfo<>(list);
		pageInfo.setTotal(list.size());
		return ServerResponse.createBySuccess(pageInfo);
	}

	@Override
	public ServerResponse<PageInfo<Score>> findBy(Integer sno_stu, String className, String courseName, Integer pageNum, Integer pageSize) {
		
		PageHelper.startPage(pageNum, pageSize);
		if(!StringUtils.isEmpty(className)){
			className = "%" + className + "%";
		}
		if(!StringUtils.isEmpty(courseName)){
			courseName = "%" + courseName + "%";
		}
		System.out.println("sno_stu===>"+sno_stu);
		List<Score> list = scoreMapper.findBy(sno_stu,className,courseName);
		PageInfo<Score> pageInfo = new PageInfo<>(list);
		pageInfo.setTotal(list.size());
		return ServerResponse.createBySuccess(pageInfo);

	}

	@Override
	public ServerResponse<String> del(Integer id) {
		Integer result = scoreMapper.deleteByPrimaryKey(id);
		if (result > 0) {
			return ServerResponse.createBySuccessMessage("删除成功");
		}else{
			return ServerResponse.createByErrorMessage("删除失败");
		}
	}

	@Override
	public ServerResponse<String> add(Integer sno,Integer score, String courseName) {
		//判断学号是否存在
		StuQuery stuQuery = new StuQuery();
		stuQuery.createCriteria().andSnoEqualTo(sno);
		Stu stu = stuMapper.selectOneByExample(stuQuery);
		if(ObjectUtils.isEmpty(stu)) return ServerResponse.createByErrorMessage("学号不存在");
		
		//判断课程是否存在
		CourseQuery courseQuery = new CourseQuery();
		courseQuery.createCriteria().andCourseNameEqualTo(courseName);
		Course course = courseMapper.selectOneByExample(courseQuery);
		if(ObjectUtils.isEmpty(course)) return ServerResponse.createByErrorMessage("课程不存在");
	
		
		//将数据封装在score上，添加到数据库
		Score scoreObject = new Score();
		scoreObject.setSno(sno);
		scoreObject.setClass_id(stu.getClassid());
		scoreObject.setCourse_id(course.getId());
		scoreObject.setScore(score);
		scoreObject.setStu_name(stu.getStudentName());
		scoreObject.setClass_name(stu.getClassName());
		scoreObject.setCourse_name(courseName);
		//学期的值暂未设置，则默认为第一学期
		scoreObject.setSeme_name("第一学期");
		Integer result = scoreMapper.insert(scoreObject);
		if (result > 0) {
			return ServerResponse.createBySuccessMessage("添加成功");
		}else{
			return ServerResponse.createByErrorMessage("添加失败");
		}
	}

	@Override
	public ServerResponse<Score> find(Integer id) {
		return ServerResponse.createBySuccess(scoreMapper.selectByPrimaryKey(id));
	}

	@Override
	public ServerResponse<String> update(Integer id, Integer sno, Integer score, String courseName) {
		Score scoreObj = new Score();
		scoreObj.setId(id);
		scoreObj.setScore(score);
		scoreObj.setCourse_name(courseName);
		int result = scoreMapper.update(scoreObj);
		if (result > 0) {
			return ServerResponse.createBySuccessMessage("修改成功");
		}else{
			return ServerResponse.createByErrorMessage("修改失败");
		}
	}

	@Override
	public ServerResponse<PageInfo<Score>> findByStu(Integer pageNum, Integer pageSize, String username, String sno) {
		if (pageNum == null){   //健壮性判断
			pageNum = 0;  //从第几条开始查询
		}
		if(pageSize == null){
			pageSize = 10;  //查询多少条  
		}
		PageHelper.startPage(pageNum,pageSize);//开始分页，PageHelper过滤器，在后面查询的话会自动在sql语句拼接一个limit，
		//自动将pagenum和size添加进去（scoreMapper.java）
		List<Score> list  = scoreMapper.findByStu(sno);
		//调用scoreMapper的findByStu方法，该方法接受学生的学号sno作为参数，并返回学生成绩的列表。
		PageInfo<Score> page = new PageInfo<>(list);
		//使用PageInfo类来封装list，以便可以获取关于分页信息的详细信息。
		page.setTotal(list.size());//设置PageInfo对象的total属性，它表示总的记录数。
		return ServerResponse.createBySuccess(page);
	}
	//查询第几页，从这页查询多少条
}



